package com.wmlt.service.impl;

import com.wmlt.dao.ArticleDao;
import com.wmlt.dao.impl.ArticleDaoImpl;
import com.wmlt.entity.Article;
import com.wmlt.entity.User;
import com.wmlt.service.ArticleService;
import com.wmlt.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ArticleServiceImpl implements ArticleService {
    ArticleDao articleDao = new ArticleDaoImpl();
    List<Article> articleList = null;
    Article article = null;
    int result = 0;
    Connection connection = null;

    @Override
    public List<Article> getAllArticle() {
        return articleDao.getAllArticle();
    }

    @Override
    public int deleteArticle(String[] articleId) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = articleDao.deleteArticle(articleId);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public List<Article> queryArticle(String articleTitle) {
        return articleDao.queryArticle(articleTitle);
    }

    //获取文章列表
    @Override
    public List<Article> getToObtain() {
        return articleDao.getToObtain();
    }

    @Override
    public int addArticle(Article article) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = articleDao.addArticle(article);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    /**
     * @description: 审核文章
     * @param: 文章数组
     * @return: int reuslt
     * @author liyuxiang
     * @date: 2021/3/22 8:44 下午
     */
    @Override
    public int reviewArticle(int[] articleId) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = articleDao.reviewArticle(articleId);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    //根据热门话题获取相对应的文章列表
    @Override
    public List<Article> getTypeLook() {
        return articleDao.getTypeLook();
    }
    //根据java获取相对应的文章列表

    @Override
    public List<Article> getTheArticleByType() {
        return articleDao.getTheArticleByType();
    }
    //根据程序人生获取相对应的文章列表

    @Override
    public List<Article> getArticlesAccordingToProgram() {
        return articleDao.getArticlesAccordingToProgram();
    }

    @Override
    public Article queryArticleByArticleId(int articleId) {
        return articleDao.queryArticleByArticleId(articleId);
    }

    @Override
    public List<Article> getArticleListByUserId(String userId) {
        return articleDao.getArticleListByUserId(userId);
    }

    @Override
    public int increaseArticleReading(String articleId, int number) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = articleDao.increaseArticleReading(articleId,number);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }
}
